<template>
	<view class="content">
		<view class="search-box">
			<u-icon name="arrow-left" color="#000000" size="18" @click="goreturn"></u-icon>
			<view class="input-box">
				<input type="text" :adjust-position="true" :placeholder="defaultKeyword" @input="inputChange"
					v-model="keyword" @confirm="doSearch(keyword)" placeholder-class="placeholder-class"
					confirm-type="search">
			</view>
			<u-icon name="search" @tap="doSearch(keyword)" color="#999" size="28"></u-icon>
		</view>
		<!--#ifdef APP -->
		<u-sticky offset-top="86">
			<!--#endif-->
			<view class="tab-box" v-if="params.name == 'Busiess'">
				<view class="tab">
					<view :class="index==0?'lan':'hei'" class="color3" @click="tab(0)">
						{{ $t('Business.Nearest') }}
						<view class="xian" v-if="index==0"></view>
					</view>
					<view :class="index==1?'lan':'hei'" class="color3" @click="tab(1)">
						{{ $t('Business.TotalViewed') }}
						<view class="xian" v-if="index==1"></view>
					</view>
					<view :class="index==2?'lan':'hei'" class="color3" @click="tab(2)">
						{{ $t('Business.Lastest') }}
						<view class="xian" v-if="index==2"></view>
					</view>
				</view>
			</view>
			<view class="tab-box" v-if="params.name == 'telBook'">
				<view class="tab">
					<view :class="index==0?'lan':'hei'" class="color3" @click="tab(0)">
						ALL
						<view class="xian" v-if="index==0"></view>
					</view>
					<view :class="index==1?'lan':'hei'" class="color3" @click="tab(1)">
						Top Viewd
						<view class="xian" v-if="index==1"></view>
					</view>
					<view :class="index==2?'lan':'hei'" class="color3" @click="tab(2)">
						Nearest
						<view class="xian" v-if="index==2"></view>
					</view>
				</view>
			</view>
			<!--#ifdef APP -->
		</u-sticky>
		<!--#endif-->
		<view class="search-keyword">
			<view class="" v-show="isShowKeywordList">
				<mescroll-body ref="mescrollRef" height="89%" @init="mescrollInit" @up="upCallback"
					v-if="params.name == 'infomation'" @down="downCallback">
					<view class="">
						<postlist :list="InfoList"></postlist>
					</view>
				</mescroll-body>
				<mescroll-body ref="mescrollRef" height="83%" @init="mescrollInit" @up="upCallback"
					v-if="params.name == 'Busiess'" @down="downCallback">
					<view class="">
						<bizlist :list="InfoList"></bizlist>
					</view>
				</mescroll-body>
				<mescroll-body ref="mescrollRef" height="83%" @init="mescrollInit" @up="upCallback"
					v-if="params.name == 'goods'" @down="downCallback">
					<view class="">
						<goodslist :list="InfoList"></goodslist>
					</view>
				</mescroll-body>
				<mescroll-body ref="mescrollRef" height="83%" @init="mescrollInit" @up="upCallback"
					v-if="params.name == 'telBook'" @down="downCallback">
					<view class="">
						<telbook :list="InfoList"></telbook>
					</view>
				</mescroll-body>
			</view>
			<scroll-view class="keyword-box" v-show="!isShowKeywordList" scroll-y :style="{height: params.name =='infomation'?'calc(100vh - 140upx)':'calc(100vh - 300upx)'}">
				<view class="keyword-block" v-if="oldKeywordList.length>0">
					<view class="keyword-list-header">
						<view>历史搜索</view>
						<view>
							<image @tap="oldDelete" src="/static/HM-search/delete.png"></image>
						</view>
					</view>
					<view class="keyword">
						<view v-for="(keyword,index) in oldKeywordList" @tap="doSearch(keyword)" :key="index">
							{{keyword}}
						</view>
					</view>
				</view>
				<view class="keyword-block" v-if="params.name == 'infomation'">
					<view class="keyword-list-header">
						<view>热门搜索</view>
						<view>
							<image @tap="hotToggle" :src="'/static/HM-search/attention'+forbid+'.png'"></image>
						</view>
					</view>
					<view class="keyword" v-if="forbid==''">
						<view v-for="(keyword,index) in hotKeywordList" @tap="doSearch(keyword)" :key="index">
							{{keyword}}
						</view>
					</view>
					<view class="hide-hot-tis" v-else>
						<view>当前搜热门搜索已隐藏</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<zero-back-top :scrollTop="scrollTop"></zero-back-top>
	</view>
</template>

<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import postlist from "@/components/postlist.vue"
	import bizlist from "@/components/bizlist.vue"
	import goodslist from "@/components/goodslist.vue"
	import telbook from "@/components/telbook.vue"
	export default {
		mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
		components: {
			postlist,
			bizlist,
			goodslist,
			telbook
		},
		data() {
			return {
				defaultKeyword: "",
				keyword: "",
				oldKeywordList: [],
				keywordList: [],
				hotKeywordList: [],
				forbid: '',
				isShowKeywordList: false,
				params: {},
				InfoList: [],
				scrollTop: 0,
				index: 0,
				id:""
			}
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop
		},
		onLoad(e) {
			this.init(e);
			this.params = e
			this.id = e.id
		},
		methods: {
			tab(e) {
				this.index = e
				this.doSearch(this.keyword)
			},
			downCallback() {
				// 下拉回调
				this.doSearch(this.keyword)
			},
			upCallback() {
				// 上拉回调
				return
			},
			goreturn() {
				uni.navigateBack({
					delta: 1
				})
			},
			//加载热门搜索
			loadHotKeyword(e) {
				console.log(e)
				if (e.name == 'infomation') {
					const params = {
						map: 'applet_community_hot_search',
						type: 2
					}
					this.$Request({
						method: 'GET',
						data: params
					}).then(res => {
						if (res.data.ec == 200) {
							//定义热门搜索关键字，可以自己实现ajax请求数据再赋值
							this.hotKeywordList = res.data.data
						} else {

						}
					})
				}
			},
			init(e) {
				this.loadDefaultKeyword();
				this.loadOldKeyword();
				this.loadHotKeyword(e);
			},
			blur() {
				uni.hideKeyboard()
			},
			//加载默认搜索关键字
			loadDefaultKeyword() {
				//定义默认搜索关键字，可以自己实现ajax请求数据再赋值,用户未输入时，以水印方式显示在输入框，直接不输入内容搜索会搜索默认关键字
				this.defaultKeyword = "Search for the keywords";
			},
			//加载历史搜索,自动读取本地Storage
			loadOldKeyword() {
				uni.getStorage({
					key: 'OldKeys',
					success: (res) => {
						var OldKeys = JSON.parse(res.data);
						this.oldKeywordList = OldKeys;
					}
				});
			},
			//监听输入
			inputChange(event) {
				//兼容引入组件时传入参数情况
				var keyword = event.detail ? event.detail.value : event;
				if (!keyword) {
					this.keywordList = [];
					this.isShowKeywordList = false;
					return;
				}
				this.isShowKeywordList = true;
				console.log(event.detail.value)
			},
			//清除历史搜索
			oldDelete() {
				uni.showModal({
					content: '确定清除历史搜索记录？',
					success: (res) => {
						if (res.confirm) {
							console.log('用户点击确定');
							this.oldKeywordList = [];
							uni.removeStorage({
								key: 'OldKeys'
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			//热门搜索开关
			hotToggle() {
				this.forbid = this.forbid ? '' : '_forbid';
			},
			//执行搜索
			doSearch(keyword) {
				if (keyword == "") {
					return
				}
				if (keyword !== false && keyword !== '') {
					this.InfoList = []
					this.isShowKeywordList = true;
				}
				keyword = keyword === false ? this.keyword : keyword;
				this.keyword = keyword;
				this.saveKeyword(keyword); //保存为历史 
				if (this.params.name == 'infomation') {
					const params = {
						map: 'applet_city_post_list',
						keyword: keyword,
						page: 0,
					}
					this.$Request({
						method: 'GET',
						data: params
					}).then(res => {
						if (res.data.ec == 200) {
							console.log(res)
							this.InfoList = res.data.data
							this.mescroll.endSuccess(res.data.data.length);
						} else {
							this.InfoList = []
							this.mescroll.endSuccess(0);
						}
					})
				}
				if (this.params.name == 'Busiess') {
					const params = {
						map: 'applet_city_new_shop_list',
						type: "nearby",
						name: keyword,
						page: 0,
						category: ""
					}
					if (this.index == 0) {
						params.type = "nearby"
					}
					if (this.index == 1) {
						params.type = "hot"
					}
					if (this.index == 2) {
						params.type = "new"
					}
					console.log(params)
					this.$Request({
						method: 'GET',
						data: params
					}).then(res => {
						console.log(res.data)
						if (res.data.ec == 200) {
							console.log(res.data.data)
							this.InfoList = res.data.data
							this.mescroll.endSuccess(res.data.data.length);
						} else {
							this.InfoList = []
							this.mescroll.endSuccess(0);
						}
					})
				}
				if (this.params.name == 'goods') {
					const params = {
						map: 'applet_goods_list',
						name: keyword,
						page: 0,
					}
					console.log(params)
					this.$Request({
						method: 'GET',
						data: params
					}).then(res => {
						console.log(res.data)
						if (res.data.ec == 200) {
							console.log(res.data.data)
							this.InfoList = res.data.data
							this.mescroll.endSuccess(res.data.data.length);
						} else {
							this.InfoList = []
							this.mescroll.endSuccess(0);
						}
					})
				}
				if (this.params.name == 'telBook') {
					const params = {
						map: 'applet_mobile_shop_list',
						sort: "new",
						name: keyword,
						page: 0,
						cate:this.id
					}
					if (this.index == 0) {
						params.sort = "new"
					}
					if (this.index == 1) {
						params.sort = "hot"
					}
					if (this.index == 2) {
						params.sort = "nearby"
					}
					console.log(params)
					this.$Request({
						method: 'GET',
						data: params
					}).then(res => {
						console.log(res.data)
						if (res.data.ec == 200) {
							console.log(res.data.data)
							this.InfoList = res.data.data
							this.mescroll.endSuccess(res.data.data.length);
						} else {
							this.InfoList = []
							this.mescroll.endSuccess(0);
						}
					})
				}
			},
			//保存关键字到历史记录
			saveKeyword(keyword) {
				uni.getStorage({
					key: 'OldKeys',
					success: (res) => {
						var OldKeys = JSON.parse(res.data);
						var findIndex = OldKeys.indexOf(keyword);
						if (findIndex == -1) {
							OldKeys.unshift(keyword);
						} else {
							OldKeys.splice(findIndex, 1);
							OldKeys.unshift(keyword);
						}
						//最多10个纪录
						OldKeys.length > 10 && OldKeys.pop();
						uni.setStorage({
							key: 'OldKeys',
							data: JSON.stringify(OldKeys)
						});
						this.oldKeywordList = OldKeys; //更新历史搜索
					},
					fail: (e) => {
						var OldKeys = [keyword];
						uni.setStorage({
							key: 'OldKeys',
							data: JSON.stringify(OldKeys)
						});
						this.oldKeywordList = OldKeys; //更新历史搜索
					}
				});
			}
		}
	}
</script>
<style lang="scss">
	view {
		display: block;
	}

	.search-box {
		width: 95%;
		background-color: rgb(248, 248, 248);
		padding: 15upx 2.5%;
		display: flex;
		justify-content: space-between;
		position: sticky;
		top: 0;
		padding-top: 40px;
		z-index: 9999999999999;

		.mSearch-input-box {
			width: 100%;
		}

		.input-box {
			width: 85%;
			flex-shrink: 1;
			display: flex;
			justify-content: center;
			align-items: center;
		}

	}

	.tab-box {
		border-bottom: 1px solid #e2e4e6;
		padding: 20rpx 0;
		margin-top: 10rpx;
		background-color: #FFFFFF;

		.tab {
			padding: 0 60rpx;
			display: flex;
			justify-content: space-between;
			font-size: 28rpx;
			font-weight: 600;
		}

		.lan {
			color: #2497ff;
		}

		.hei {
			color: #333333;
		}

		.xian {
			width: 60%;
			height: 10rpx;
			background-color: #2497ff;
			border-radius: 20rpx;
			position: absolute;
			left: 50%;
			transform: translate(-50%, 100%);
		}

	}

	.tab>view {
		position: relative;
	}

	.search-box .input-box>input {
		width: 100%;
		height: 60upx;
		font-size: 32upx;
		border: 0;
		border-radius: 60upx;
		-webkit-appearance: none;
		-moz-appearance: none;
		appearance: none;
		padding: 0 3%;
		margin: 0;
		background-color: #ffffff;
	}

	.placeholder-class {
		color: #999;
		font-size: 26rpx;
	}

	.search-keyword {
		width: 100%;
		background-color: rgb(242, 242, 242);
	}

	.keyword-list-box {
		height: calc(100vh - 110upx);
		padding-top: 10upx;
		border-radius: 20upx 20upx 0 0;
		background-color: #fff;
	}

	.keyword-entry-tap {
		background-color: #eee;
	}

	.keyword-entry {
		width: 94%;
		height: 80upx;
		margin: 0 3%;
		font-size: 30upx;
		color: #333;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: solid 1upx #e7e7e7;
	}

	.keyword-entry image {
		width: 60upx;
		height: 60upx;
	}

	.keyword-entry .keyword-text,
	.keyword-entry .keyword-img {
		height: 80upx;
		display: flex;
		align-items: center;
	}

	.keyword-entry .keyword-text {
		width: 90%;
	}

	.keyword-entry .keyword-img {
		width: 10%;
		justify-content: center;
	}

	.keyword-box {
		height: calc(100vh - 140upx);
		border-radius: 20upx 20upx 0 0;
		background-color: #fff;
	}

	.keyword-box .keyword-block {
		padding: 10upx 0;
	}

	.keyword-box .keyword-block .keyword-list-header {
		width: 94%;
		padding: 10upx 3%;
		font-size: 27upx;
		color: #333;
		display: flex;
		justify-content: space-between;
	}

	.keyword-box .keyword-block .keyword-list-header image {
		width: 40upx;
		height: 40upx;
	}

	.keyword-box .keyword-block .keyword {
		width: 94%;
		padding: 3px 3%;
		display: flex;
		flex-flow: wrap;
		justify-content: flex-start;
	}

	.keyword-box .keyword-block .hide-hot-tis {
		display: flex;
		justify-content: center;
		font-size: 28upx;
		color: #6b6b6b;
	}

	.keyword-box .keyword-block .keyword>view {
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 60upx;
		padding: 0 20upx;
		margin: 10upx 20upx 10upx 0;
		height: 60upx;
		font-size: 28upx;
		background-color: rgb(248, 248, 248);
		color: #6b6b6b;
	}
</style>
